﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMP_QuanLySV.DAL;

namespace SMP_QuanLySV.BLL
{
    class BLLSinhVien
    {
        List<SinhVien> list;
        QuanLySVDataContext db;

        // constructor
        public BLLSinhVien()
        {
            this.list = new QuanLySVDataContext().SinhViens.ToList();
            this.db = new QuanLySVDataContext();
        }

        // kiem tra 1 sinh vienc co ton tai hay khong
        public bool checkExist(SinhVien temp)
        {
            foreach (SinhVien sv in list)
            {
                if (sv.SVID.Equals(temp.SVID))
                    return true;
            }
            return false;
        }

        public List<SinhVien> BindingSV(string ID, string ten)
        {
            if (ID != "" && ten == "")
            {
                return (from c in db.SinhViens
                        where (c.SVID.Contains(ID))
                        select c).ToList();
            }
            if (ID == "" && ten != "")
            {
                return (from c in db.SinhViens
                        where (c.Ten.Contains(ten) || c.HoLot.Contains(ten))
                        select c).ToList();
            }
            if (ID != "" && ten != "")
            {
                return (from c in db.SinhViens
                        where (c.SVID.Contains(ID) && (c.Ten.Contains(ten) || c.HoLot.Contains(ten)))
                        select c).ToList();
            }
            return list;
        }

        // update thong tin sinh vien
        public bool updateSinhVien(SinhVien update)
        {
            try
            {

                var temp = (from c in db.SinhViens
                            where c.SVID == update.SVID
                            select c).First();
                SinhVien found = (SinhVien)temp;
                found.SVID = update.SVID;
                found.HoLot = update.HoLot;
                found.Ten = update.Ten;
                found.LopID = update.LopID;
                found.NgaySinh = update.NgaySinh;
                found.NoiSinh = update.NoiSinh;
                found.GioiTinh = update.GioiTinh;
                found.DiaChi = update.DiaChi;
                found.Email = update.Email;
                found.DienThoai = update.DienThoai;
                found.NamTNPT = update.NamTNPT;
                found.DanTocID = update.DanTocID;
                found.TonGiaoID = update.TonGiaoID;
                found.QuocTich = update.QuocTich;
                found.NgheNghiepCha = update.NgheNghiepCha;
                found.HoTenCha = update.HoTenCha;
                found.HoTenMe = update.HoTenMe;
                found.NgheNghiepMe = update.NgheNghiepMe;
                found.GhiChu = update.GhiChu;


                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // them sinh vien moi vao database
        public bool insertSinhVien(SinhVien temp)
        {
            try
            {
                db.SinhViens.InsertOnSubmit(temp);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // xoa sinh vien
        public bool deleteSinhVien(SinhVien temp)
        {
            try
            {
                var dt = (from c in db.SinhViens
                          where c.SVID == temp.SVID
                          select c).First();
                SinhVien found = (SinhVien)dt;

                db.SinhViens.DeleteOnSubmit(found);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // tra ve danh sach cac sinh vien
        public List<SinhVien> returnListSinhVien()
        {
            this.list = new QuanLySVDataContext().SinhViens.ToList();
            return list;
        }

        // tra ve 1 sinh vien theo so thu tu trong list
        public SinhVien retrieveByIndex(int i)
        {
            this.list = new QuanLySVDataContext().SinhViens.ToList();
            if (i >= 0 && i < list.Count)
                return list[i];
            else return null;
        }

        // tra ve 1 sinh vien theo ma sinh vien
        public SinhVien retrieveByID(string ID)
        {
            this.list = new QuanLySVDataContext().SinhViens.ToList();
            foreach (SinhVien temp in list)
                if (temp.SVID == ID)
                {
                    return temp;
                }
            return null;
        }

        // kiem tra thong tin 1 sinh vien co hop le hay khong
        public bool kiemTra(SinhVien temp)
        {
            if (temp.SVID == "") return false;
            return true;
        }
    }
}
